﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Admin_Reports : System.Web.UI.Page
{
    BankDataDataContext db = new BankDataDataContext();
    public struct Key
    {
        public int month;
        public int year;

        public Key(int _month, int _year)
        {
            month = _month;
            year = _year;
        }
    };

    public struct Data
    {
        public int creditsCount;
        public decimal amount;
        public decimal payedMainAmount;
        public decimal payedPercentAmount;
    };

    protected void Page_Load(object sender, EventArgs e)
    {
        AddItemsToDateList();
        setValues();
    }

    protected void AddItemsToDateList()
    {
        int startYear = 2011;
        int startMonth = 1;
        int endYear = DateTime.Now.Year;
        int endMonth = DateTime.Now.Month;
        if (DateList.Items.Count == 0)
        {
            for (int year = startYear; year <= endYear; year++)
            {
                for (int month = startMonth;
                    ((month < 13 && year != endYear) || (month <= endMonth && year == endYear));
                    month++)
                {
                    DateTime dateTime = new DateTime(year, month, 1);
                    ListItem listItem = new ListItem(dateTime.ToString("MMMM yyyy"), dateTime.ToString("MMMM yyyy"));
                    DateList.Items.Add(listItem);
                }
            }
            DateList.SelectedIndex = DateList.Items.Count - 1;
        }
    }

    public Dictionary<Key, Data> GenerateCreditReportDictioanry()
    {
        Dictionary<Key, Data> dictionary = new Dictionary<Key, Data>();
        foreach (Contract contract in db.Contracts)
        {
            Key key = new Key(contract.CreationDate.Month, contract.CreationDate.Year);
            if (dictionary.ContainsKey(key))
            {
                Data value;
                dictionary.TryGetValue(key, out value);
                value.creditsCount++;
                value.amount += (contract.Amount / (decimal)contract.Currency.USDExchangeRate);
                dictionary.Remove(key);
                dictionary.Add(key, value);
            }
            else
            {
                Data data = new Data();
                data.creditsCount = 1;
                data.amount = contract.Amount / (decimal)contract.Currency.USDExchangeRate;
                dictionary.Add(key, data);
            }
        }
        foreach (ContractPayment payment in db.ContractPayments)
        {
            Key key = new Key(payment.Date.Month, payment.Date.Year);
            if (dictionary.ContainsKey(key))
            {
                Data value;
                dictionary.TryGetValue(key, out value);
                value.payedMainAmount += (payment.MainAmount / (decimal)payment.Currency.USDExchangeRate);
                value.payedPercentAmount += (payment.PercentageAmount / (decimal)payment.Currency.USDExchangeRate);
                dictionary.Remove(key);
                dictionary.Add(key, value);
            }
            else
            {
                Data data = new Data();
                data.payedMainAmount = (payment.MainAmount / (decimal)payment.Currency.USDExchangeRate);
                data.payedPercentAmount = (payment.PercentageAmount / (decimal)payment.Currency.USDExchangeRate);
                dictionary.Add(key, data);
            }
        }
        return dictionary;
    }

    protected void DropDownList1_DataBound(object sender, EventArgs e)
    {

    }

    protected void setValues()
    {
        Dictionary<Key, Data> dictionary = GenerateCreditReportDictioanry();
        DateTime date = DateTime.Parse((DateList.SelectedValue));
        Key key = new Key(date.Month, date.Year);
        Data value;
        if (dictionary.TryGetValue(key, out value))
        {
            CreditCount.Text = value.creditsCount.ToString();
            CreditAmount.Text = value.amount.ToString() + " USD";
            MainPayed.Text = value.payedMainAmount.ToString() + " USD";
            PercentagePaid.Text = value.payedPercentAmount.ToString() + " USD";
        }
        else
        {
            CreditCount.Text = "0";
            CreditAmount.Text = "0 USD";
            MainPayed.Text = "0 USD";
            PercentagePaid.Text = "0 USD";
        }
    }

    protected void DateList_SelectedIndexChanged(object sender, EventArgs e)
    {
        setValues();
    }
}